<template>
  <n-space>
    <n-switch type="primary" @click="toEdit=!toEdit">
      <template #checked>
        隐藏
      </template>
      <template #unchecked>
        编辑
      </template>
    </n-switch>
    <n-radio-group
        v-model:value="value"
        name="radiobuttongroup1"
        :disabled="!toEdit"
    >
      <n-space>
        <n-radio
            v-for="song in songs"
            :key="song.value"
            :value="song.value"
        >
          {{ song.label }}
        </n-radio>
        <n-button
            type="primary"
            size="tiny"
            dashed
            @click="plus = plus-1"
        >
          上一{{ dayUnitMap.get(value) }}
        </n-button>
        <n-button
            type="primary"
            size="tiny"
            dashed
            @click="plus = plus+1"
        >
          下一{{ dayUnitMap.get(value) }}
        </n-button>
        <n-switch v-model:value="notRecord" >
          <template #unchecked>
            添加未记录
          </template>
        </n-switch>
      </n-space>
    </n-radio-group>
  </n-space>
  <IdTimeBar
      :data="barData"
      :key="key"
  ></IdTimeBar>
  <SemiCircleTimePie
      :data="timePieData"
      :type="2"
      :key="key + 15"
  ></SemiCircleTimePie>
</template>
<script lang="ts" setup>

import useUserStore from "@/store/modules/user";
import {UseConstsStore} from "@/store/modules/consts";
import UseSurfaceSettingStore from "@/store/modules/surfaceSetting";
import {useMessage} from "naive-ui";
import {post, Response} from "@/api/http";
import {addFields, getTakeTime4Stat} from "@/api/url";
import {onMounted, ref, watch} from "vue";
import SemiCircleTimePie from "@/views/taskoperation/tasklog/common/SemiCircleTimePie.vue";
import IdTimeBar from "@/views/field/statpanel/common/IdTimeBar.vue";
import {obj2Map} from "@/utils";

const userStore = useUserStore()
const useConsts = UseConstsStore()
const useSurfaceSetting = UseSurfaceSettingStore()
const message = useMessage()

const key = ref(Date.now())
const toEdit = ref(false)
const value = ref('4')
const songs = useConsts.getLabelValue(useConsts.consts.dayUnitMap).filter(l=>l.value!=='1')
const dayUnitMap = obj2Map(useConsts.consts.dayUnitMap)
const plus = ref(0)
const barData = ref<any>({series:[],timeRange:[]});
const timePieData = ref([])
const notRecord = ref(false);

const getInfo = async ()=>{
  try {
    // table.tableLoading.value = true;
    const data: Response = await post({
      url: getTakeTime4Stat,
      data: {type:3,plus:plus.value,dayUnit:parseInt(value.value),notRecord:notRecord.value},
      token:userStore.getToken()
    });
    console.log(data.data)
    barData.value = data.data.data
    timePieData.value = barData.value.series.map((value1:any) => {
      let times = 0;
      value1.takeTimeList.forEach((v:any) => (times += v));
      return { name: value1.name, value: times };
    });
  }catch(e) {
    message.error((e as Error).message)
  }finally {
    key.value = new Date().getTime();
  }
}
onMounted(getInfo)
watch(plus,getInfo)
watch(value,getInfo)
watch(notRecord,getInfo)
</script>